package com.study.book.gym.char03;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author MI
 * @version 1.0
 * @date 2021/5/13 22:29
 */
public class ThreadPoolDemo {
    public static void main(String[] args) throws InterruptedException {
        final MyTask myTask = new MyTask();
        final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS,
                new SynchronousQueue<Runnable>(),
                r -> {
                    final Thread thread = new Thread(r);
                    thread.setDaemon(true);
                    System.out.println("ctreate " + thread);
                    return thread;
                }
        );

        for (int i = 0; i < 5; i++) {
            threadPoolExecutor.submit(myTask);
        }
        TimeUnit.SECONDS.sleep(2);
    }

    public static class MyTask implements Runnable {
        @Override
        public void run() {
            System.out.println(System.currentTimeMillis()
                    + ":Thread ID:" + Thread.currentThread().getId()
            );

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
